//
//  HELCreditStatisticsVC.m
//  NursingAssociation
//
//  Created by wsj2015 on 2017/3/27.
//  Copyright © 2017年 wsj2015. All rights reserved.
//

#import "HELCreditStatisticsVC.h"
#import "HELCreditStatisticsCell.h"
#import "HELCreditStatisticsModel.h"
@interface HELCreditStatisticsVC ()
@property (nonatomic,  assign) BOOL isFirstGetData;


@end

@implementation HELCreditStatisticsVC



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.isFirstGetData = YES;
    self.tableView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64-44);
    [self.view addSubview:self.tableView];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:@"HELCreditStatisticsCell" bundle:nil] forCellReuseIdentifier:@"HELCreditStatisticsCell"];
    
    
    weakSelf_define;
    [self.tableView wsj_HeaderRefreshBlock:^{
        [weakSelf getCreditRecordList:YES];
    }];
    [self.tableView wsj_FooterRefreshBlock:^{
        [weakSelf getCreditRecordList:NO];
    }];
    
    
}

- (void)requestDataFirst{
    if (self.isFirstGetData) {
        [self getCreditRecordList:YES];
        self.isFirstGetData = NO;
    }
}

- (void)getCreditRecordList:(BOOL)isRecover{
    
    static NSUInteger page = 1;
    if (isRecover) {
        page =1;
    }else{
        page ++;
    }
    
    NSMutableDictionary *params =[NSMutableDictionary dictionary];
    params[@"page"] = @(page);
    params[@"rows"] = @(15);
    
    weakSelf_define;
    self.tableView.loading = YES;
    [[HELNetworkManager sharedMg] POST:helCreditYearsStatistics parameters:params success:^(id responseObject) {
        
        [weakSelf.tableView.header endRefreshing];
        [weakSelf.tableView.footer endRefreshing];
        weakSelf.tableView.loading = NO;
        if ([responseObject[@"data"] isKindOfClass:[NSDictionary class]]) {
            
            if (isRecover) {
                [weakSelf.dataArray removeAllObjects];
            }
            NSInteger totalPage = 0;
            if ([responseObject[@"data"][@"totalPage"] isKindOfClass:[NSNull class]]) {
                
            }else{
                totalPage =[responseObject[@"data"][@"totalPage"] integerValue];
            }
            
            NSInteger currentPage = [responseObject[@"data"][@"page"] integerValue];
            if (currentPage>=totalPage) {
                [weakSelf.tableView.footer noticeNoMoreData];
            }else{
                [weakSelf.tableView.footer resetNoMoreData];
            }
            
            [weakSelf.dataArray addObjectsFromArray:[HELCreditStatisticsModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"data"]]];
            
            [weakSelf.tableView reloadData];
            
        }
        
        
    } failure:^(NSString *err) {
        [weakSelf.tableView.header endRefreshing];
        [weakSelf.tableView.footer endRefreshing];
        weakSelf.tableView.loading = NO;
    }];
   
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 42;
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
   
    if (section == 0) {
        
        UIView *div =[UIView new];
        div.backgroundColor = [UIColor whiteColor];
        div.frame = CGRectMake(0, 0, kScreenWidth, 42);
        NSArray *titleArr = @[@"年    份",@"一    类", @"二    类"];
        CGFloat w = kScreenWidth / 3;
        for (int i = 0; i < 3; i ++) {
            
            UILabel *label = [UILabel new];
            
            label.text = titleArr[i];
            label.font = [UIFont boldSystemFontOfSize:15];
            label.textColor = [UIColor blackColor];
            
            label.frame = CGRectMake(w * i, 6, w, 30);
            label.textAlignment = NSTextAlignmentCenter;
            [div addSubview:label];
            
        }
        
        return div;
        
    }
    return nil;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return  39;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HELCreditStatisticsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HELCreditStatisticsCell"];
    HELCreditStatisticsModel *model = self.dataArray[indexPath.row];
    cell.centerL.text = model.oneClass;
    cell.left.text =  model.date;
    cell.right.text = model.twoClass;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
}

@end
